/*
 * 热搜
 * */
import {observable, action} from 'mobx';
import axiosUtil from '../utils/axiosUtil';
import AppStore from './appStore';
import AppConfig from '../config/appConfig';
import viewUtil from '../utils/viewUtil';
import {TimeStamp,IsNullObject} from '../utils/utils';
import md5 from 'md5';
import {CreateHaoDanwangList,CreatePingDuoDuoList,CreateJinDongXiangList} from '../mode/modeCtr';

class HotSearchStore {
  @observable keyWords = [];
  @observable list_id = ''  //拼多多单用：翻页时建议填写前页返回的list_id值
  /**
   * 申请热搜词
   * */
  @action applyKeyWords() {
    if (this.keyWords.length > 0) {
      return;
    }
    this.apply_HaoDanKu();
  }

  /**
   * 好单网数据热搜词
   * */
  @action apply_HaoDanKu() {
    var url = AppStore.data.hotKey.url.format(AppStore.data.taoBaoApi.haoDanKey)
    console.log(url)
    axiosUtil.get(url).then((res) => {
      if (IsNullObject(res)) return
      if (parseInt(res.code)  === 1) {
        this.keyWords = [];
        res.data.map((item) => {
          this.keyWords.push(item.keyword);
        });

      }
    });
  }

  /**
   * 获取搜索数据
   * */
  getSuperSearch(tp, sort, productName, pageIndex = 1) {
    //大类型
    if (tp === AppConfig.TaoBao) {
      return this.getSuperSearch_HaoDanWang(productName, sort, pageIndex);
    } else if (tp === AppConfig.JinDong) {
      return  this.getSuperSearch_JinDong_Xiang(productName, sort, pageIndex)
    } else if (tp === AppConfig.PingDuoDuo) {
      return this.getSuperSearch_PingDuoDuo(productName, sort, pageIndex);
    } else {
      viewUtil.ShowToast('不存在搜索～');
      return new Promise(() => {
        return null;
      });
    }
  }

  /**
   * 获取搜索数据_好单网(淘宝)
   * */
  getSuperSearch_HaoDanWang(productName, sort, pageIndes) {
    return new Promise((resolve, reject) => {
      const url = AppStore.data.taoBaoApi.SuperSearch.format(
        AppStore.data.taoBaoApi.haoDanKey,
        productName,
        pageIndes,
        sort,
      );

      axiosUtil.get(url).then((res) => {
        if (res.code === 1) {
          if (res.data === null || res.data === []) {
            viewUtil.ShowToast('服务器错误～');
          } else {
            console.log(res)
            let ttt = CreateHaoDanwangList(res.data);
            resolve({data: ttt, pageIndex: res.min_id});
          }
        } else {
          viewUtil.ShowToast('服务器错误～');
        }
      });
    });
  }

  /**
   *获取京东搜索数据_京东——京享
   * */

  getSuperSearch_JinDong_Xiang(productName, sort, pageIndes){
    return new Promise(resolve => {
        var sortItem = AppConfig.GetJinXiangSortType_JinDong(sort);
        const timestamp = TimeStamp();
        var  itemSearchDto = {}

        itemSearchDto.itemName = productName
        itemSearchDto.saleType = 10
        itemSearchDto.sourceChannel = 80
        itemSearchDto.supportCoupon = 1
        itemSearchDto.sortMethod = sortItem.sort
        itemSearchDto.orderByType = sortItem.orderByType
        itemSearchDto.jdSaleMethod = 100

      var  url = AppStore.data.jinDongApi.SuperSearch +timestamp+"&pageNum="+pageIndes+"&pageSize="+20+"&itemSearchDto="+JSON.stringify(itemSearchDto)

      axiosUtil
        .get(url).then(res=>{

         if (res===null){
           viewUtil.ShowToast('服务器错误～');
         }else {
            if (res.code !=='0' || res.data === null){
              viewUtil.ShowToast('服务器错误～');
            }else {
              var itemlist =   CreateJinDongXiangList(res.data.result)
              resolve({data: itemlist, pageIndex: res.data.nextPage});
            }
         }
      })
    })
  }

  /**
   * 获取搜索数据_拼多多
   * */
  getSuperSearch_PingDuoDuo(productName, sort, pageIndes) {
    return new Promise((resolve, reject) => {
      sort = AppConfig.GetPingDuoDuoSortType(sort)

      const timestamp = TimeStamp();
      const sign =
        AppStore.data.pingDuoDuoApi.client_secret +
        'client_id' +
        AppStore.data.pingDuoDuoApi.client_id +
        'keyword' +
        productName +
        'list_id'+this.list_id+
        'page' +
        pageIndes +
        'page_size' +
        50 +
        'sort_type' +
        sort +
        'timestamp' +
        timestamp +
        'type' +
        AppStore.data.pingDuoDuoApi.SuperSearch
        +'with_coupon'+'true'+ //有优惠卷才返回
        AppStore.data.pingDuoDuoApi.client_secret;
      const signback = md5(sign);

      let data ={}
      data.client_id= AppStore.data.pingDuoDuoApi.client_id
      data.keyword= productName
      data.page= pageIndes
      data.page_size= 50
      data.sort_type= sort
      data.timestamp=timestamp
      data.type=AppStore.data.pingDuoDuoApi.SuperSearch
      data.with_coupon=true
      data.list_id=this.list_id
      data.sign=signback.toUpperCase()

      axiosUtil
        .post(AppStore.data.pingDuoDuoApi.api, data)
        .then((res) => {
           // console.log(JSON.stringify(res));
          if (res.error_response){
            viewUtil.ShowToast('服务器错误～');
          } else {
            if (res.goods_search_response.goods_list.length <=0){
              viewUtil.ShowToast('没有找到商品!');
              resolve({data: [], pageIndex: pageIndes});
            } else {
              this.list_id =  res.goods_search_response.list_id
              var itemlist =  CreatePingDuoDuoList(res.goods_search_response.goods_list)
              resolve({data: itemlist, pageIndex: pageIndes+1});
            }
          }
        });
    });
  }
}

const store = new HotSearchStore();

export default store;
